package com.android.lovingpet.common.userinfo;

import android.os.Bundle;
import android.support.annotation.Nullable;

import com.android.lovingpet.base.BasePresenter;
import com.android.lovingpet.common.Back;
import com.android.lovingpet.common.UserModel;
import com.android.lovingpet.common.postlist.PostListPresenter;

import java.util.ArrayList;

public class UserInfoPresenter extends BasePresenter<UserInfoContract.View> implements UserInfoContract.Presenter {

    private UserInfoContract.View view;

    public UserInfoPresenter(UserInfoContract.View view) {
        this.view = view;
    }

    @Override
    public void getFans(int id) {
        UserModel.getInstance().getFansOrAttentions(id, 0, new Back() {
            @Override
            public void onFailure(String meg) {
                view.showToast(meg);
            }

            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                ArrayList<Integer> ids = bundle.getIntegerArrayList("ids");
                if (ids == null) {
                    ids = new ArrayList<>();
                }
                view.setFans(ids);
            }
        });
    }

    @Override
    public void getAttentions(int id) {
        UserModel.getInstance().getFansOrAttentions(id, 1, new Back() {
            @Override
            public void onFailure(String meg) {
                view.showToast(meg);
            }

            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                ArrayList<Integer> ids = bundle.getIntegerArrayList("ids");
                if (ids == null) {
                    ids = new ArrayList<>();
                }
                view.setAttentions(ids);
            }
        });
    }

    @Override
    public void followOrCancel(int targetId, final int mode) {
        UserInfoModel.getInstance().followOrCancel(targetId, mode, new Back() {
            @Override
            public void onFailure(String meg) {
                view.showToast(meg);
            }

            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                view.setButtonText(mode == 0);//mode是0表示关注成功，显示取消关注
            }
        });
    }
}
